/* eslint-disable no-var */
'use strict';

const Controller = require('egg').Controller;
const { ObjectId } = require('mongodb');
class SubjectController extends Controller {
  async index() {
    const { ctx } = this;
    var { id } = ctx.params;
    var url = ctx.origin + '/public';
    try {
      var db = this.app.mongodb;
      var res = await db.collection('article').find({ _id: ObjectId(id) }).toArray();
      res.forEach(item => {
        item.imgSrc = url + item.imgSrc;
        item.avatar = url + item.avatar;
        item.headImgSrc = url + item.headImgSrc;
      });
      ctx.body = {
        status: 200,
        res,
      };
    } catch (err) {
    //   后端报错
      this.app.logger.error(err);
      ctx.status = 400;
      ctx.body = {
        status: 400,
        msg: '输入的id值不合法',
      };
    }
  }
}

module.exports = SubjectController;
